<template>
  <div class="app-container home">
    <el-row :gutter="20">
      <!-- 新歌播放量 -->
      <el-col :span="12">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>新歌播放量</span>
          </div>
          <div class="chart-container">
            <div ref="newSongsChart" style="width: 100%; height: 300px"></div>
          </div>
        </el-card>
      </el-col>

      <!-- 音乐分类分布 -->
      <el-col :span="12">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>音乐分类分布</span>
          </div>
          <div class="chart-container">
            <div
              ref="musicCategoryChart"
              style="width: 100%; height: 300px"
            ></div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="mt-20">
      <!-- 热门歌曲排行榜 -->
      <el-col :span="24">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>热门歌曲排行榜</span>
          </div>
          <el-table :data="topSongs" style="width: 100%">
            <el-table-column
              prop="rank"
              label="排名"
              width="80"
            ></el-table-column>
            <el-table-column prop="songName" label="歌曲名称"></el-table-column>
            <el-table-column prop="singer" label="歌手"></el-table-column>
            <el-table-column prop="playCount" label="播放量"></el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import * as echarts from "echarts";
import {
  getHotSongList,
  getNewSongs,
  getEachTypeSongListCount,
} from "@/api/song/song";
import { getSongTypeList } from "@/api/song_category/song_category";

export default {
  name: "Index",
  data() {
    return {
      eachTypeSongListCount: [],
      songTypeList: [],
      NewSongs: [],
      // 热门歌曲
      topSongs: [],

      // 图表实例
      newSongsChart: null,
      musicCategoryChart: null,
    };
  },
  mounted() {
    this.initCharts();
    this.getHotSongList();
    this.getNewSongs();
    this.getEachTypeSongListCount();
    this.getSongTypeList();
  },
  methods: {
    getSongTypeList() {
      getSongTypeList().then((res) => {
        this.songTypeList = res;
        this.updateMusicCategoryChart();
      });
    },
    getEachTypeSongListCount() {
      getEachTypeSongListCount().then((res) => {
        this.eachTypeSongListCount = res;
        console.log(this.eachTypeSongListCount);
        this.updateMusicCategoryChart();
      });
    },
    // 获取热门歌曲列表
    getHotSongList() {
      getHotSongList().then((res) => {
        this.topSongs = res.sort((a, b) => b.playCount - a.playCount);
        //获取前五个
        this.topSongs = this.topSongs.slice(0, 5);
        this.topSongs.forEach((item, index) => {
          item.rank = index + 1;
        });
      });
    },
    getNewSongs() {
      getNewSongs().then((res) => {
        this.NewSongs = res;
        this.updateNewSongsChart();
      });
    },
    updateNewSongsChart() {
      if (this.newSongsChart) {
        // 按播放量排序并取前7首
        const sortedSongs = [...this.NewSongs]
          .sort((a, b) => b.playCount - a.playCount)
          .slice(0, 7);

        const option = {
          title: {
            text: "新歌播放量TOP" + sortedSongs.length,
            top: 10,
            left: "center",
          },
          tooltip: {
            trigger: "axis",
            axisPointer: {
              type: "shadow",
            },
            formatter: function (params) {
              const data = params[0];
              return `${data.name}<br/>播放量：${data.value}<br/>歌手：${data.data.singer}`;
            },
          },
          grid: {
            left: "15%",
            right: "5%",
            bottom: "15%",
            top: "20%",
            containLabel: true,
          },
          xAxis: {
            type: "value",
            name: "播放量",
            nameLocation: "middle",
            nameGap: 25,
            axisLabel: {
              formatter: function (value) {
                if (value >= 10000) {
                  return (value / 10000).toFixed(1) + "万";
                }
                return value;
              },
              fontSize: 10,
            },
            nameTextStyle: {
              fontSize: 12,
            },
          },
          yAxis: {
            type: "category",
            data: sortedSongs.map((item) => item.songName),
            name: "歌曲名称",
            nameTextStyle: {
              fontSize: 12,
            },
            axisLabel: {
              interval: 0,
              fontSize: 10,
              formatter: function (value) {
                return value.length > 6 ? value.substring(0, 6) + "..." : value;
              },
            },
          },
          series: [
            {
              name: "播放量",
              type: "bar",
              barWidth: "60%",
              data: sortedSongs.map((item) => ({
                value: item.playCount,
                singer: item.singer,
              })),
              itemStyle: {
                color: "#409EFF",
              },
              label: {
                show: true,
                position: "right",
                fontSize: 10,
                formatter: function (params) {
                  const value = params.value;
                  if (value >= 10000) {
                    return (value / 10000).toFixed(1) + "万";
                  }
                  return value;
                },
              },
            },
          ],
        };
        this.newSongsChart.setOption(option);
      }
    },
    updateMusicCategoryChart() {
      if (
        this.musicCategoryChart &&
        this.songTypeList.length > 0 &&
        this.eachTypeSongListCount.length > 0
      ) {
        const option = {
          title: {
            text: "音乐分类分布",
            top: 10,
            left: "center",
          },
          tooltip: {
            trigger: "item",
            formatter: "{a} <br/>{b}: {c}首 ({d}%)",
          },
          legend: {
            orient: "vertical",
            left: 10,
            data: this.eachTypeSongListCount.map((item) => item.songType),
          },
          series: [
            {
              name: "歌曲数量",
              type: "pie",
              radius: ["50%", "70%"],
              avoidLabelOverlap: false,
              label: {
                show: true,
                position: "outside",
                formatter: function (params) {
                  return params.data.songType + ": " + params.value + "首";
                },
                fontSize: 12,
              },
              emphasis: {
                label: {
                  show: true,
                  fontSize: "20",
                  fontWeight: "bold",
                },
              },
              labelLine: {
                show: true,
                length: 20,
                length2: 10,
              },
              data: this.eachTypeSongListCount.map((item) => ({
                value: item.count,
                songType: item.songType,
              })),
            },
          ],
        };
        this.musicCategoryChart.setOption(option);
      }
    },
    initCharts() {
      // 新歌播放量图
      this.newSongsChart = echarts.init(this.$refs.newSongsChart);
      this.updateNewSongsChart();

      // 音乐分类分布图
      this.musicCategoryChart = echarts.init(this.$refs.musicCategoryChart);
    },
  },
};
</script>

<style scoped lang="scss">
.home {
  .mt-20 {
    margin-top: 20px;
  }

  .box-card {
    margin-bottom: 20px;
  }

  .chart-container {
    width: 100%;
    height: 300px;
  }
}
</style>
